package com.woke.addressactivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.http.HttpMethods;
import com.woke.method.MyApp;
import com.zhongcai.online.R;
import com.zxing.encoding.EncodingHandler;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import cn.xl.xxl.imagecache.ImageLoader;

public class UserPayActivity extends Activity {

	private ImageView mIermae;

	private MyApp myapp;

	private ImageLoader imageLoader;
	private int screenHigth, screenWidth;
	private TextView title ,text_money;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alipapapay);
		imageLoader = new ImageLoader(this);
		myapp = (MyApp) getApplication();
		screenHigth = myapp.screenHigth;
		screenWidth = myapp.screenWidth;
		mIermae = (ImageView) this.findViewById(R.id.avpay_image_code);
		text_money = (TextView) findViewById(R.id.avpay_text_monye);
		LayoutParams params = mIermae.getLayoutParams();
		params.width = screenWidth / 5 * 4;
		params.height = screenWidth / 5 * 4;
		mIermae.setLayoutParams(params);
		findViewById(R.id.user_er_back).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View view) {
						finish();
					}
				});
		// 读取数据
		Intent intent = getIntent();
		String payWay=intent.getStringExtra("payWay");
		final String url = intent.getStringExtra("url");
		String money = intent.getStringExtra("money");
		title = (TextView) findViewById(R.id.fmuser_text_title);	
		if (payWay.equals("1")) {
			title.setText("微信收款");
		}else {
			title.setText("支付宝收款");
		}
		text_money.setText("￥"+money);
		try {
			
			if (!url.equals("")) {
				
				Bitmap qrCodeBitmap = EncodingHandler.createQRCode(url, 350);
				mIermae.setImageBitmap(qrCodeBitmap);
			}else {
				Toast.makeText(UserPayActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
			}
			
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    

	}

	private void dopost(String payWay, String totalAmount) {
		if(myapp.getDatas_load ()==null)return;
		// 封装数据
		Map<String, String> parmas = new HashMap<String, String>();
		parmas.put("userId", myapp.getDatas_load().getId());
		parmas.put("payWay", payWay);
		parmas.put("totalAmount", totalAmount);

		DefaultHttpClient client = new DefaultHttpClient();// http客户端
		HttpPost httpPost = new HttpPost(HttpMethods.BASEURL
				+ "g=api&m=Scancode&a=paySmServlet");
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		if (parmas != null) {
			Set<String> keys = parmas.keySet();
			for (Iterator<String> i = keys.iterator(); i.hasNext();) {
				String key = (String) i.next();
				pairs.add(new BasicNameValuePair(key, parmas.get(key)));
			}
		}

		try {
			UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
					"utf-8");

			// 将POST数据放入HTTP请求

			httpPost.setEntity(p_entity);

			// 发出实际的HTTP POST请求

			HttpResponse response = client.execute(httpPost);
			HttpEntity entity = response.getEntity();
			InputStream content = entity.getContent();
			String returnConnection = convertStreamToString(content);
			System.out.println("returnConnection============="
					+ returnConnection);
			// {"flag":"success","info":"OK","data":{"data":"weixin:\/\/wxpay\/bizpayurl?pr=5ocvA51"},"time":1482751227}
			try {
				JSONObject jsonObject = new JSONObject(returnConnection);
				final String flag = jsonObject.getString("flag");
				if (flag.equals("success")) {
					JSONObject json = jsonObject.getJSONObject("data");
					String url = json.getString("data");
					
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
			
				e.printStackTrace();
			}
			
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private String convertStreamToString(InputStream content) {

		// TODO Auto-generated method stub
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				content));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				content.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	private void urlPost(String url){
		System.out.println("url---------------------"+url);
		try {
			if (!url.equals("")) {
				
				Bitmap qrCodeBitmap = EncodingHandler.createQRCode(url, 350);
				mIermae.setImageBitmap(qrCodeBitmap);
			}else {
				Toast.makeText(UserPayActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
			}
			
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
	}
	


	
	
}
